package com.cnstats.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.visualization.client.AjaxLoader;
import com.google.gwt.visualization.client.DataTable;
import com.google.gwt.visualization.client.Query;
import com.google.gwt.visualization.client.QueryResponse;
import com.google.gwt.visualization.client.Query.Callback;
import com.google.gwt.visualization.client.visualizations.ColumnChart;
import com.google.gwt.visualization.client.visualizations.Visualization;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Home implements EntryPoint {
	private final class ClkSearch implements ClickListener {
		private final class CBQuery implements Callback {
			public void onResponse(QueryResponse response) {
				if (response.isError()) {
					Window.alert(response.getMessage());
				} else {
					// Get the data from the QueryResponse.
					DataTable data = response.getDataTable();
					colChart.draw(data);
				}
			}
		}

		public void onClick(Widget sender) {
//			String dataUrl = "http://cnstatus.appspot.com/query";
			String dataUrl = "http://localhost:8080/query";
			dataUrl += "?dims=" + tbDims.getText();
			dataUrl += "&name=" + tbName.getText();
			dataUrl += "&dfmt=" + tbDfmt.getText();
			
			lblQuery.setText(dataUrl);
			Query query = Query.create(dataUrl);
			query.send(new CBQuery());
		}
	}
	
	private final class ClkClear implements ClickListener {
		public void onClick(Widget sender) {
			colChart.draw(null);
//			plChart.clear();
//			plChart.add(colChart);
		}
	}
	
	private final class VisLoaded implements Runnable {
		public void run() {
			colChart = new ColumnChart();
			Visualization.setSize(colChart.getElement(), 800, 400);
			plChart.add(colChart);
		}
	}

	private VerticalPanel plMain = new VerticalPanel();
	private HorizontalPanel plQuery = new HorizontalPanel();
	private Label lblDims = new Label("Dims:");
	private TextBox tbDims = new TextBox();
	private Label lblName = new Label("Name:");
	private TextBox tbName = new TextBox();
	private Label lblDfmt = new Label("DateFmt:");
	private TextBox tbDfmt = new TextBox();
	private Button btnQuery = new Button("Search", new ClkSearch());
	private Button btnClear = new Button("Clear", new ClkClear());
	private VerticalPanel plChart = new VerticalPanel();
	private Label lblQuery = new Label();
	private ColumnChart colChart;
	
  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {
  	plQuery.add(lblDims);
  	plQuery.add(tbDims);
  	plQuery.add(lblName);
  	plQuery.add(tbName);
  	plQuery.add(lblDfmt);
  	plQuery.add(tbDfmt);
  	plQuery.add(btnQuery);
  	plQuery.add(btnClear);
  	plChart.add(lblQuery);
  	plMain.add(plQuery);
  	plMain.add(plChart);
  	tbDims.setText("all");
  	tbName.setText("gdp");
  	tbDfmt.setText("%Y");
  	RootPanel.get().add(plMain);
		AjaxLoader.loadVisualizationApi(new VisLoaded(), ColumnChart.PACKAGE);
  }
  
  private ColumnChart.Options createOptions() {
		ColumnChart.Options options = ColumnChart.Options.create();
		options.setWidth(800);
		options.setHeight(400);
		options.set3D(false);
		options.setTitle("GDP");
		options.setColors("blue");
		return options;
	}
}


